#!/bin/sh /etc/rc.common

START=80

cat /etc/config/cpufreq_scaling &> /dev/null
if [ $? == 1 ]; then echo > /etc/config/cpufreq_scaling; fi

uci get cpufreq_scaling.@cpufreq[0] &> /dev/null
if [ $? == 1 ]; then
while uci -q delete cpufreq_scaling.@cpufreq[0]; do :; done
uci -q commit cpufreq_scaling
uci -q batch <<-EOF
 set cpufreq_scaling.cpufreq="cpufreq"
 set cpufreq_scaling.@cpufreq[0].corekeeper="4"
 set cpufreq_scaling.@cpufreq[0].min_freq="480000"
 set cpufreq_scaling.@cpufreq[0].max_freq="1008000"
 set cpufreq_scaling.@cpufreq[0].governor="ondemand"
 set cpufreq_scaling.@cpufreq[0].upthreshold="25"
 set cpufreq_scaling.@cpufreq[0].factor="10"
EOF
sleep 1
uci -q commit cpufreq_scaling
fi

unescape=$(uci get cpufreq_scaling.@cpufreq[0].corekeeper)
if [ "$unescape" == "1" ]; then corekeeper=1 forcor="3 2 1"
elif [ "$unescape" == "2" ]; then corekeeper=2 forcor="3 2" refrcor=1
elif [ "$unescape" == "3" ]; then corekeeper=3 forcor=3 refrcor="2 1"
elif [ "$unescape" == "4" ]; then corekeeper=4 refrcor="3 2 1"; fi
if [ "$corekeeper" -lt "4" ]; then for i in $forcor; do echo 0 >/sys/devices/system/cpu/cpu${i}/online; done; fi
if [ "$corekeeper" -gt "1" ]; then for i in $refrcor; do echo 1 >/sys/devices/system/cpu/cpu${i}/online; done; fi

min_freq=$(uci get cpufreq_scaling.@cpufreq[0].min_freq)
max_freq=$(uci get cpufreq_scaling.@cpufreq[0].max_freq)
governor=$(uci get cpufreq_scaling.@cpufreq[0].governor)
upthreshold=$(uci get cpufreq_scaling.@cpufreq[0].upthreshold)
factor=$(uci get cpufreq_scaling.@cpufreq[0].factor)

scaling_max_freq=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq)
scaling_min_freq=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq)
Filter="480000|648000|816000|1008000"

uci get cpufreq_scaling.@cpufreq[0].min_freq &> /dev/null
if [ $? == 1 ]; then  # Есть ли min_freq?
 uci set cpufreq_scaling.@cpufreq[0].min_freq="480000"
 uci commit cpufreq_scaling
elif echo $Filter | grep -qE $min_freq; then
:
else
 uci set cpufreq_scaling.@cpufreq[0].min_freq="480000"
 uci commit cpufreq_scaling
fi


start() {
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies &> /dev/null
if [ $? != 1 ]; then

if [[ "$min_freq" != "$scaling_min_freq" || "$max_freq" != "$scaling_max_freq" ]]; then
 echo $governor > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
 echo $max_freq > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
 echo $min_freq > /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq
 echo $upthreshold > /sys/devices/system/cpu/cpufreq/ondemand/up_threshold
 echo $factor > /sys/devices/system/cpu/cpufreq/ondemand/sampling_down_factor
 echo 1 > /sys/devices/system/cpu/cpufreq/ondemand/io_is_busy
fi

fi

}

